import { queryBasicProfile, queryAdvancedProfile,queryTask,queryMachineList} from '../services/api';

export default {
  namespace: 'profile',

  state: {
    workresults:[],
    domains:[],
    clicks:[],
    task:{
      is_running:false,
    },
    agents:[]
  },

  effects: {
    *fetchBasic({payload}, { call, put }) {
      const results = yield call(queryBasicProfile,payload);
      yield put({
        type: 'showworkresults',
        payload: results.content,
      });
    },
    *fetchAgent({payload}, { call, put }) {
      const results = yield call(queryMachineList,payload);
      yield put({
        type: 'showagent',
        payload: results.content,
      });
    },
    *fetchAdvanced({payload}, { call, put }) {
      var task_id = payload.task_id
      const task = yield call(queryTask,payload);
      yield put({
        type: 'showtask',
        payload: task.content,
      });
      const domains = yield call(queryAdvancedProfile,{task_id:task_id,type:"DOMAIN"});
      yield put({
        type: 'showdomains',
        payload: domains.content,
      });
      const clicks = yield call(queryAdvancedProfile,{task_id:task_id,type:"CLICK"});
      yield put({
        type: 'showclicks',
        payload: clicks.content,
      });
     
    },
    *fetchAdvanced2({payload}, { call, put }) {
      const response = yield call(queryAdvancedProfile,payload);
      
    },
  },

  reducers: {
    showdomains(state, { payload }) {
      return {
        ...state,
        damains:payload,
      };
    },
    showclicks(state, { payload }) {
      return {
        ...state,
        clicks:payload,
      };
    },
    showtask(state,{payload}) {
      return {
        ...state,
        task:payload,
      }
    },
    showworkresults(state,{payload}) {
      return {
        ...state,
        workresults:payload,
      }
    },
    showagent(state,{payload}) {
      return {
        ...state,
        agents:payload,
      }
    },
  },
};
